package com.elione.modbus;

/**
 * ModbusSignal provides signal information which will be used by ModbusTcpMaster  
 * 
 * @author     Zhang hongyu <hongyu.zhang@natcon7.com>
 * 
 * @version    1.0 (06.04.2005)
 * 
 * @version    1.1 (12.04.2005) 
 *  - new clone method impelmented
 * @version    1.2 (09.10.2009 zha)
 * - add new property name;  
 */
public class ModbusSignal {

    /* constant to indicate WORD */
	public static final int MTYPE_W  = 100;     
    /* constant to indicate DWORD */  
    public static final int MTYPE_DW = 200;
    
    /** 
     * Create a empty NC2ModbusSignal by default setting 
     * <br> - address     = 1 
     * <br> - mtype       = 1
     * <br> - arraySize   = 1
     * <br> - arraySize2d = 0
     * <br> - writepointAddress = 0     
     * @since   1.0
     */
    public ModbusSignal() {
      address      = 1;
      mtype        = MTYPE_W;
      arraysize    = 1;
      arraysize2d  = 0;      
      writepointAddress = 0;     
      readyAddress = 0;    
      IEEformat = false;      
    }

    /** 
     * Create a empty ModbusSignal with given setting. The reset property use defautl setting 
     * <br> - arraySize2d = 0
     * <br> - writepointAddress = 0
     *    
     * @param   add - signal address 
     * @param   vt  - signal type , 
     * @param   arraysize - first dimension array size, for no-array signal should be 1 
     *
     * @since   1.0
     */  
    public ModbusSignal(int add, int vt, int arrsize) {
      address    = add;
      mtype      = vt;
      arraysize  = arrsize;
      arraysize2d  = 0;
      writepointAddress = 0; 
      readyAddress = 0;
      IEEformat = false;        
    }    
    /** 
     * Create a empty ModbusSignal with given setting. The reset property use defautl setting 
     * <br> - arraySize2d = 0
     * <br> - writepointAddress = 0
     *    
     * @param   add - signal address 
     * @param   vt  - signal type , 
     * @param   arraysize - first dimension array size, for no-array signal should be 1
     * @param   isIEE - if use IEE format to retrieve value, e.g. convert DWORD to REAL value  
     *
     * @since   1.0
     */  
    public ModbusSignal(int add, int vt, int arrsize,boolean isIEE) {
      address    = add;
      mtype      = vt;
      arraysize  = arrsize;
      arraysize2d  = 0;
      writepointAddress = 0; 
      readyAddress = 0;
      IEEformat = isIEE;        
    }     
    /**
	 * @return Returns the address.
	 */
	public int getAddress() {
		return address;
	}
	/**
	 * @param address The address to set.
	 */
	public void setAddress(int address) {
		this.address = address;
	}
	/**
	 * @return Returns the arraysize.
	 */
	public int getArraysize() {
		return arraysize;
	}
	/**
	 * @param arraysize The arraysize to set.
	 */
	public void setArraysize(int arraysize) {
		this.arraysize = arraysize;
	}
	/**
	 * @return Returns the arraysize2d.
	 */
	public int getArraysize2d() {
		return arraysize2d;
	}
	/**
	 * @param arraysize2d The arraysize2d to set.
	 */
	public void setArraysize2d(int arraysize2d) {
		this.arraysize2d = arraysize2d;
	}
	/**
	 * @return Returns the mtype.
	 */
	public int getMtype() {
		return mtype;
	}
	/**
	 * @param mtype The mtype to set.
	 */
	public void setMtype(int mtype) {
		this.mtype = mtype;
	}
	/**
	 * @return Returns the readyAddress.
	 */
	public int getReadyAddress() {
		return readyAddress;
	}
	/**
	 * @param readyAddress The readyAddress to set.
	 */
	public void setReadyAddress(int readyAddress) {
		this.readyAddress = readyAddress;
	}
	/**
	 * @return Returns the writepointAddress.
	 */
	public int getWritepointAddress() {
		return writepointAddress;
	}
	/**
	 * @param writepointAddress The writepointAddress to set.
	 */
	public void setWritepointAddress(int writepointAddress) {
		this.writepointAddress = writepointAddress;
	}
    /**
     * @return Returns the iEEformat.
     */
    public boolean isIEEformat() {
        return IEEformat;
    }
    /**
     * @param eformat The iEEformat to set.
     */
    public void setIEEformat(boolean eformat) {
        IEEformat = eformat;
    }    
    
    
	/**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /** 
     * Return string as: [address]-[mtype]-[arraysize]-[arraysize2d] -[IEEFormat]-[writepointadd]
     *
     * @since   1.0
     * 
     * @return 
     */  
	public String toString() {
		String info = address + "-";
		info += mtype + "-";    
        info += arraysize + "-";       
        info += arraysize2d + "-";       
        info += IEEformat + "-";
        info += writepointAddress + "-";
        info += readyAddress;
        
        return(info);  
	}    
    
    
    protected Object clone() throws CloneNotSupportedException {
        ModbusSignal newObject = new ModbusSignal();
        newObject.address = this.address;
        newObject.mtype = this.mtype;
        newObject.arraysize = this.arraysize;
        newObject.arraysize2d = this.arraysize2d;
        newObject.writepointAddress = this.writepointAddress; 
        newObject.IEEformat = this.IEEformat;
        return newObject;        
    }    
    
	private int address;       // signal modbus address
	private int mtype;         // signal modbus type, word or dword 
	private int arraysize;     // siganl first dimension array size 
	private int arraysize2d;   // signal second dimension array size  
	private int writepointAddress; // address for writepoint to access 2D array variable 
	private int readyAddress;            // address of status siganl. used when reading 2D array
    private boolean IEEformat; // flag of if signal value is using IEE format     
    
    private String name; 
}
